function GD = getCollectionData(Q, collection_id)

%given a coolection_id, function will try to extract data belonging to that
%collection. 
%Q: key and url as a struct i.e. Q.key, Q.url
%collection_id: id of the collection to retreive data from

%%R0mesh Stanislaus May 2010 //summer of change//

if isnumeric(collection_id)
    collection_id = num2str(collection_id);
end
V.collection_id = collection_id;
SI = selectCommon(Q, 'items', V);

code7 = 0; %error something happend when retreiving Items

if isstruct(SI)
    %get size and see if any is there
    siS = size(SI,1);
    if siS == 0
        code7 = 1; %empty collectio
    else
        code7 = 3; %all fine for now
    end
else
    code7 = 1; %not a struct probably empty
end

switch code7
    case 0
        error('Error retreiving items from collection...see if items exist in collection');
    case 1
        GD = 'Empty'; %empty collection 
        %error('Empty Collection');
    case 3
        %process the data using subfunctions getMore
        item_id_s = {SI(:).item_id};
        %get the statements linked to thses items....
        dataStruct='';
        for m =1:size(item_id_s,2)
            SS.item_id = item_id_s{m};
            try
                S = selectCommon(Q, 'statements' ,SS);
                %the value field has item_id for next level
                XX.item_id = S(1).value;
                %get specific info for this <item>
                XXI = selectCommon(Q, 'items', XX); %XXI.entity contains the whether it is an Data item or Analysis item
                %get all related <statements> for this item
                XXS = selectCommon(Q, 'statements', XX);
                %[{XXS(:).value}] %for testing comment if u want
                for u = 1:size(XXS,1)
                    dataStruct(m).(XXI.entity).(cleanUp4Struct(XXS(u).object))= XXS(u).value;
                end
            catch
                %the call did not returned a 0x1 struct array with fields.
                %THus there is nothing and is assumed that at the point of
                %data entry something went wrong...For the time being that
                %entry is ignored...in futue and note would be added
                %U can identify the empty struct it by using the isempty function...
                disp('Please check data struct. Most likely data were not entered properly...');
            end
        end
        GD=dataStruct;%success
    otherwise
        error('ERROR: Check collection id...');
end
        

%GD=1;

% function K = getMore(SI, fName)
% 
% %this subfunction is used to get more data as need be by main prog
% %this gets an struct with many fields
% %SI: struct file with many field
% %fName: name of the field of interest e.g. item_id
% 
% for i=1:size(SI,1)
%     %get the item_id for the entry and create a struct for geting more data
%     try
%         K = getfield(SI(i),fName);
%         
%     catch
%         %no more data...
%     end
% end
% 
% 
% 
